package com.ruicar.afs.cloud.apply.pre.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.app.condition.ApplyAppCommonProblemCondition;
import com.ruicar.afs.cloud.apply.pre.app.condition.ApplyAppOpinionFeedbackCondition;
import com.ruicar.afs.cloud.apply.pre.app.entity.ApplyAppOpinionFeedback;
import com.ruicar.afs.cloud.apply.pre.app.enums.AppEnum;
import com.ruicar.afs.cloud.apply.pre.app.mapper.ApplyAppOpinionFeedbackMapper;
import com.ruicar.afs.cloud.apply.pre.app.service.ApplyAppOpinionFeedbackService;
import com.ruicar.afs.cloud.apply.pre.approve.utils.ImageUploadUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.image.config.GwtFastdfsProperties;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.mapper.ComAttachmentManagementMapper;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.image.store.feign.ImageStoreApiFeign;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: app意见反馈
 * @author: mingzhi.li
 * @date: 2020/8/13 15:07
 */
@Service
@AllArgsConstructor
@Slf4j
public class ApplyAppOpinionFeedbackServiceImpl extends ServiceImpl<ApplyAppOpinionFeedbackMapper, ApplyAppOpinionFeedback> implements ApplyAppOpinionFeedbackService {

    private final ImageUploadUtil getTempDir;
    private final GwtFastdfsProperties gwtFastdfsProperties;
    private final ImageStoreApiFeign imageStoreApiFeign;
    private final ComAttachmentManagementMapper managementMapper;
    private final ComAttachmentFileService comAttachmentFileService;

    @Override
    public boolean attachmentUpload(ApplyAppOpinionFeedbackCondition condition) {
        Map<String, String> headers = new HashMap<>(3);
        headers.put("Authorization", this.getTempDir.getAuthorization());
        String file = AppEnum.PICTURE_OPINION.getIndex();
        String fileName = AppEnum.PICTURE_OPINION.getName();
        //app图片管理附件上传
        for(int i=0;i<condition.getFile().length;i++){
            Map map = getTempDir.doUpload(condition.getFile()[i], fileName, headers);
            this.setImageStore(file, map, condition.getId().toString());
            log.info("app图片管理附件上传成功:{}", condition.getId().toString());
        }

        return false;
    }

    /**
     * 获取token信息
     *
     * @return
     */
    public String getAuthorization() {
        Map<String, String> data = new HashMap<>();
        data.put("client_id", this.gwtFastdfsProperties.getClientId());
        data.put("client_secret", this.gwtFastdfsProperties.getClientSecret());
        data.put("grant_type", this.gwtFastdfsProperties.getGrantType());
        JSONObject jsonObject = this.imageStoreApiFeign.getTokenInfo(data);
        String authorization = jsonObject.getString("token_type") + " " + jsonObject.getString("access_token");
        return authorization;
    }

    /**
     * 保存MD5到file
     *
     * @param s
     */
    public void setImageStore(String s, Map<String, String> map, String params) {
        //查询manage文件表内的数据
        ComAttachmentFile comAttachmentFile = new ComAttachmentFile();
        ComAttachmentManagement comAttachmentManagement = this.managementMapper.selectOne(Wrappers.<ComAttachmentManagement>query().lambda()
                .eq(ComAttachmentManagement::getUniqueCode, s));
        if (ObjectUtil.isNotNull(comAttachmentManagement)) {
            comAttachmentFile.setBelongNo(params);
            comAttachmentFile.setBusiNo(params);
            comAttachmentFile.setAttachmentCode(String.valueOf(comAttachmentManagement.getId()));
            comAttachmentFile.setAttachmentName(comAttachmentManagement.getAttachmentName());
            comAttachmentFile.setFileStatus("standard");
            comAttachmentFile.setFileType("jpg");
            comAttachmentFile.setFileId(map.get("md5"));
            comAttachmentFile.setHistoryVersion(ApplyConstants.ZERO);
            comAttachmentFile.setIsElectronic(ApplyConstants.ZERO);
            comAttachmentFile.setFileName(comAttachmentManagement.getAttachmentName() + ".jpg");
            comAttachmentFile.setUploadTime(new Date());
            comAttachmentFile.setCreateBy(SecurityUtils.getUsername());
            //存到file表
            this.comAttachmentFileService.save(comAttachmentFile);
            log.info("{}", comAttachmentManagement.getAttachmentName() + "上传成功");
        }
    }
}
